import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
// import { OptionalAuthGuard } from '@wwjCommon/security/guards/optional-auth.guard';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { AuthService } from '../../../login/services/api/auth.service';
import { MemberLogService } from '../../services/api/member-log.service';
import { MemberService } from '../../services/api/member.service';

/**
 * MemberController
 * 对应 PHP: Member Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('member')
@Controller('api/member')
@UseGuards(ApiCheckTokenGuard)
export class MemberController {
  constructor(
    private readonly authService: AuthService,
    private readonly memberLogService: MemberLogService,
    private readonly memberService: MemberService,
  ) {}

  /**
   * info
   * 路由: GET member
   * PHP路由: Route::get('member', 'member.Member/info')
   */
  @Get('member')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: 'info' })
  async info(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.memberService.getInfo(id);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * center
   * 路由: GET center
   * PHP路由: Route::get('center', 'member.Member/center')
   */
  @Get('center')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: 'center' })
  async center(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: center

      return await this.memberService.center();
    } catch (error) {
      throw new BusinessException('center操作失败', error);
    }
  }

  /**
   * modify
   * 路由: PUT modify/:field
   * PHP路由: Route::put('modify/:field', 'member.Member/modify')
   */
  @Put('modify/:field')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: 'modify' })
  async modify(
    @Param('field') field: string,
    @Body() data: ModifyDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: modify

      return await this.memberService.modify(field, data['value']);
    } catch (error) {
      throw new BusinessException('modify操作失败', error);
    }
  }

  /**
   * edit
   * 路由: PUT edit
   * PHP路由: Route::put('edit', 'member.Member/edit')
   */
  @Put('edit')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: 'edit' })
  async edit(@Body() data: EditDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.memberService.edit(data['data']);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * mobile
   * 路由: PUT mobile
   * PHP路由: Route::put('mobile', 'member.Member/mobile')
   */
  @Put('mobile')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: 'mobile' })
  async mobile(@Body() data: MobileDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: mobile

      return await this.authService.bindMobile(
        data['mobile'],
        data['mobile_code'],
      );
    } catch (error) {
      throw new BusinessException('mobile操作失败', error);
    }
  }

  /**
   * qrcode
   * 路由: GET qrcode
   * PHP路由: Route::get('qrcode', 'member.Member/qrcode')
   */
  @Get('qrcode')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: 'qrcode' })
  async qrcode(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: qrcode

      return await this.memberService.getQrcode();
    } catch (error) {
      throw new BusinessException('qrcode操作失败', error);
    }
  }
}
